msg_tool\scripts\softpal\arc/
pac.rs

1//! Softpal PAC archive (.pac)
2use super::*;
3use crate::ext::io::*;
4use crate::scripts::base::*;
5use crate::types::*;
6use anyhow::{Result, anyhow, ensure};
7use std::io::{Read, Seek, SeekFrom};
8use std::sync::{Arc, Mutex};
9
10const SOFTPAL_INDEX_OFFSET: u64 = 0x3FE;
11const AMUSE_INDEX_OFFSET: u64 = 0x804;
12const XOR_KEY: u32 = 0xF7D5859D;
13
14#[derive(Debug, Clone, Copy)]
15enum SoftpalPacVariant {
16    Softpal,
17    Amuse,
18}
19
20#[derive(Debug)]
21/// Softpal PAC archive builder.
22pub struct SoftpalPacBuilder {
23    variant: SoftpalPacVariant,
24}
25
26impl SoftpalPacBuilder {
27    /// Creates a builder for the classic Softpal PAC layout.
28    pub fn new() -> Self {
29        Self {
30            variant: SoftpalPacVariant::Softpal,
31        }
32    }
33
34    /// Creates a builder for the Amuse Craft PAC layout.
35    pub fn new_amuse() -> Self {
36        Self {
37            variant: SoftpalPacVariant::Amuse,
38        }
39    }
40}
41
42impl ScriptBuilder for SoftpalPacBuilder {
43    fn default_encoding(&self) -> Encoding {
44        Encoding::Cp932
45    }
46
47    fn default_archive_encoding(&self) -> Option<Encoding> {
48        Some(Encoding::Cp932)
49    }
50
51    fn build_script(
52        &self,
53        buf: Vec<u8>,
54        _filename: &str,
55        _encoding: Encoding,
56        archive_encoding: Encoding,
57        config: &ExtraConfig,
58        _archive: Option<&Box<dyn Script>>,
59    ) -> Result<Box<dyn Script>> {
60        Ok(Box::new(SoftpalPacArchive::new(
61            MemReader::new(buf),
62            archive_encoding,
63            config,
64            self.variant,
65        )?))
66    }
67
68    fn build_script_from_file(
69        &self,
70        filename: &str,
71        _encoding: Encoding,
72        archive_encoding: Encoding,
73        config: &ExtraConfig,
74        _archive: Option<&Box<dyn Script>>,
75    ) -> Result<Box<dyn Script>> {
76        let file = std::fs::File::open(filename)?;
77        let reader = std::io::BufReader::new(file);
78        Ok(Box::new(SoftpalPacArchive::new(
79            reader,
80            archive_encoding,
81            config,
82            self.variant,
83        )?))
84    }
85
86    fn build_script_from_reader(
87        &self,
88        reader: Box<dyn ReadSeek>,
89        _filename: &str,
90        _encoding: Encoding,
91        archive_encoding: Encoding,
92        config: &ExtraConfig,
93        _archive: Option<&Box<dyn Script>>,
94    ) -> Result<Box<dyn Script>> {
95        Ok(Box::new(SoftpalPacArchive::new(
96            reader,
97            archive_encoding,
98            config,
99            self.variant,
100        )?))
101    }
102
103    fn extensions(&self) -> &'static [&'static str] {
104        &["pac"]
105    }
106
107    fn script_type(&self) -> &'static ScriptType {
108        match self.variant {
109            SoftpalPacVariant::Softpal => &ScriptType::SoftpalPac,
110            SoftpalPacVariant::Amuse => &ScriptType::SoftpalPacAmuse,
111        }
112    }
113
114    fn is_archive(&self) -> bool {
115        true
116    }
117
118    fn is_this_format(&self, _filename: &str, buf: &[u8], buf_len: usize) -> Option<u8> {
119        match self.variant {
120            SoftpalPacVariant::Softpal => None,
121            SoftpalPacVariant::Amuse => {
122                if buf_len >= 4 && buf.starts_with(b"PAC ") {
123                    Some(10)
124                } else {
125                    None
126                }
127            }
128        }
129    }
130}
131
132#[derive(Debug, Clone)]
133struct SoftpalPacEntry {
134    name: String,
135    offset: u32,
136    size: u32,
137}
138
139#[derive(Debug)]
140/// Softpal PAC archive reader.
141pub struct SoftpalPacArchive<T: Read + Seek + std::fmt::Debug> {
142    reader: Arc<Mutex<T>>,
143    entries: Vec<SoftpalPacEntry>,
144}
145
146impl<T: Read + Seek + std::fmt::Debug> SoftpalPacArchive<T> {
147    fn new(
148        mut reader: T,
149        archive_encoding: Encoding,
150        _config: &ExtraConfig,
151        variant: SoftpalPacVariant,
152    ) -> Result<Self> {
153        let encoding = match archive_encoding {
154            Encoding::Auto => Encoding::Cp932,
155            other => other,
156        };
157        let file_len = reader.stream_length()?;
158        if let SoftpalPacVariant::Amuse = variant {
159            let signature = reader.peek_u32_at(0)?;
160            ensure!(
161                signature == 0x2043_4150,
162                "Invalid Softpal PAC/Amuse signature: {signature:08X}"
163            );
164        }
165
166        let count_offset = match variant {
167            SoftpalPacVariant::Softpal => 0,
168            SoftpalPacVariant::Amuse => 8,
169        };
170        let count = reader.peek_i32_at(count_offset)?;
171        ensure!(count >= 0, "Negative entry count: {count}");
172        let count = count as usize;
173
174        if count == 0 {
175            return Ok(Self {
176                reader: Arc::new(Mutex::new(reader)),
177                entries: Vec::new(),
178            });
179        }
180
181        let (index_offset, name_length) = match variant {
182            SoftpalPacVariant::Softpal => {
183                let mut chosen = None;
184                for &candidate in &[0x20usize, 0x10usize] {
185                    let first_offset =
186                        reader.peek_u32_at(SOFTPAL_INDEX_OFFSET + candidate as u64 + 4)? as u64;
187                    let expected = SOFTPAL_INDEX_OFFSET + (candidate as u64 + 8) * count as u64;
188                    if first_offset == expected {
189                        ensure!(
190                            first_offset <= file_len,
191                            "First entry offset {first_offset:#X} exceeds archive length {file_len:#X}"
192                        );
193                        chosen = Some((SOFTPAL_INDEX_OFFSET, candidate));
194                        break;
195                    }
196                }
197                chosen.ok_or_else(|| anyhow!("Unsupported Softpal PAC layout"))?
198            }
199            SoftpalPacVariant::Amuse => {
200                let name_length = 0x20usize;
201                let first_offset =
202                    reader.peek_u32_at(AMUSE_INDEX_OFFSET + name_length as u64 + 4)? as u64;
203                let expected = AMUSE_INDEX_OFFSET + (name_length as u64 + 8) * count as u64;
204                ensure!(
205                    first_offset == expected,
206                    "Invalid Softpal PAC/Amuse index layout: expected {expected:#X}, got {first_offset:#X}"
207                );
208                ensure!(
209                    first_offset <= file_len,
210                    "First entry offset {first_offset:#X} exceeds archive length {file_len:#X}"
211                );
212                (AMUSE_INDEX_OFFSET, name_length)
213            }
214        };
215
216        reader.seek(SeekFrom::Start(index_offset))?;
217        let mut entries = Vec::with_capacity(count);
218        for _ in 0..count {
219            let name = reader.read_fstring(name_length, encoding, true)?;
220            let size = reader.read_u32()?;
221            let offset = reader.read_u32()?;
222            let end = offset as u64 + size as u64;
223            ensure!(
224                end <= file_len,
225                "Entry '{name}' exceeds archive bounds: offset={offset:#X}, size={size:#X}"
226            );
227            entries.push(SoftpalPacEntry { name, offset, size });
228        }
229
230        Ok(Self {
231            reader: Arc::new(Mutex::new(reader)),
232            entries,
233        })
234    }
235}
236
237impl<T: Read + Seek + std::fmt::Debug + 'static> Script for SoftpalPacArchive<T> {
238    fn default_output_script_type(&self) -> OutputScriptType {
239        OutputScriptType::Json
240    }
241
242    fn default_format_type(&self) -> FormatOptions {
243        FormatOptions::None
244    }
245
246    fn is_archive(&self) -> bool {
247        true
248    }
249
250    fn iter_archive_filename<'a>(
251        &'a self,
252    ) -> Result<Box<dyn Iterator<Item = Result<String>> + 'a>> {
253        Ok(Box::new(
254            self.entries.iter().map(|entry| Ok(entry.name.clone())),
255        ))
256    }
257
258    fn iter_archive_offset<'a>(&'a self) -> Result<Box<dyn Iterator<Item = Result<u64>> + 'a>> {
259        Ok(Box::new(
260            self.entries.iter().map(|entry| Ok(entry.offset as u64)),
261        ))
262    }
263
264    fn open_file<'a>(&'a self, index: usize) -> Result<Box<dyn ArchiveContent + 'a>> {
265        let entry = self
266            .entries
267            .get(index)
268            .ok_or_else(|| anyhow!("Index out of bounds: {index}"))?;
269        let mut buf = [0u8; 16];
270        let buflen = self.reader.cpeek_at(entry.offset as u64, &mut buf)?;
271        let script_type = detect_script_type(&entry.name, &buf[..buflen]);
272        if buflen >= 16 && should_decrypt_entry(&buf) {
273            let mut data = vec![0u8; entry.size as usize];
274            self.reader.cpeek_exact_at(entry.offset as u64, &mut data)?;
275            decrypt_entry(&mut data);
276            Ok(Box::new(MemEntry::new(
277                entry.name.clone(),
278                data,
279                script_type,
280            )))
281        } else {
282            Ok(Box::new(PacEntry::new(
283                entry.clone(),
284                self.reader.clone(),
285                script_type,
286            )))
287        }
288    }
289}
290
291fn should_decrypt_entry(data: &[u8]) -> bool {
292    data.len() > 16 && data[0] == b'$'
293}
294
295fn decrypt_entry(data: &mut [u8]) {
296    if data.len() <= 16 {
297        return;
298    }
299    let mut shift: u32 = 4;
300    for chunk in data[16..].chunks_exact_mut(4) {
301        let mut block = [0u8; 4];
302        block.copy_from_slice(chunk);
303        let rotate = (shift & 7) as u32;
304        block[0] = block[0].rotate_left(rotate);
305        shift = shift.wrapping_add(1);
306        let decrypted = u32::from_le_bytes(block) ^ XOR_KEY;
307        chunk.copy_from_slice(&decrypted.to_le_bytes());
308    }
309}
310
311#[derive(Debug)]
312struct MemEntry {
313    name: String,
314    data: Vec<u8>,
315    pos: usize,
316    script_type: Option<ScriptType>,
317}
318
319impl MemEntry {
320    pub fn new(name: String, data: Vec<u8>, script_type: Option<ScriptType>) -> Self {
321        Self {
322            name,
323            data,
324            pos: 0,
325            script_type,
326        }
327    }
328}
329
330impl ArchiveContent for MemEntry {
331    fn name(&self) -> &str {
332        &self.name
333    }
334
335    fn script_type(&self) -> Option<&ScriptType> {
336        self.script_type.as_ref()
337    }
338}
339
340impl Read for MemEntry {
341    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
342        if self.pos >= self.data.len() {
343            return Ok(0);
344        }
345        let bytes_to_read = buf.len().min(self.data.len() - self.pos);
346        if bytes_to_read == 0 {
347            return Ok(0);
348        }
349        buf[..bytes_to_read].copy_from_slice(&self.data[self.pos..self.pos + bytes_to_read]);
350        self.pos += bytes_to_read;
351        Ok(bytes_to_read)
352    }
353}
354
355impl Seek for MemEntry {
356    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
357        let len = self.data.len() as i64;
358        let current = self.pos as i64;
359        let new_pos = match pos {
360            SeekFrom::Start(offset) => offset as i64,
361            SeekFrom::End(offset) => len + offset,
362            SeekFrom::Current(offset) => current + offset,
363        };
364        if new_pos < 0 || new_pos > len {
365            return Err(std::io::Error::new(
366                std::io::ErrorKind::InvalidInput,
367                "Seek position is out of bounds",
368            ));
369        }
370        self.pos = new_pos as usize;
371        Ok(self.pos as u64)
372    }
373
374    fn stream_position(&mut self) -> std::io::Result<u64> {
375        Ok(self.pos as u64)
376    }
377}
378
379#[derive(Debug)]
380struct PacEntry<T: Read + Seek + std::fmt::Debug> {
381    header: SoftpalPacEntry,
382    pos: u64,
383    reader: Arc<Mutex<T>>,
384    script_type: Option<ScriptType>,
385}
386
387impl<T: Read + Seek + std::fmt::Debug> PacEntry<T> {
388    fn new(
389        header: SoftpalPacEntry,
390        reader: Arc<Mutex<T>>,
391        script_type: Option<ScriptType>,
392    ) -> Self {
393        Self {
394            header,
395            pos: 0,
396            reader,
397            script_type,
398        }
399    }
400}
401
402impl<T: Read + Seek + std::fmt::Debug> ArchiveContent for PacEntry<T> {
403    fn name(&self) -> &str {
404        &self.header.name
405    }
406
407    fn script_type(&self) -> Option<&ScriptType> {
408        self.script_type.as_ref()
409    }
410}
411
412impl<T: Read + Seek + std::fmt::Debug> Read for PacEntry<T> {
413    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
414        if self.pos >= self.header.size as u64 {
415            return Ok(0);
416        }
417        let bytes_to_read = buf.len().min((self.header.size as u64 - self.pos) as usize);
418        if bytes_to_read == 0 {
419            return Ok(0);
420        }
421        let bytes_read = self.reader.cpeek_at(
422            self.header.offset as u64 + self.pos,
423            &mut buf[..bytes_to_read],
424        )?;
425        self.pos += bytes_read as u64;
426        Ok(bytes_read)
427    }
428}
429
430impl<T: Read + Seek + std::fmt::Debug> Seek for PacEntry<T> {
431    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
432        let len = self.header.size as i64;
433        let current = self.pos as i64;
434        let new_pos = match pos {
435            SeekFrom::Start(offset) => offset as i64,
436            SeekFrom::End(offset) => len + offset,
437            SeekFrom::Current(offset) => current + offset,
438        };
439        if new_pos < 0 || new_pos > len {
440            return Err(std::io::Error::new(
441                std::io::ErrorKind::InvalidInput,
442                "Seek position is out of bounds",
443            ));
444        }
445        self.pos = new_pos as u64;
446        Ok(self.pos as u64)
447    }
448
449    fn stream_position(&mut self) -> std::io::Result<u64> {
450        Ok(self.pos as u64)
451    }
452}